<?php
class PhongtraokhoasController extends AppController {

	var $name = 'Phongtraokhoas';
	var $uses = array('Phongtraokhoa','Hocky','Namhoc'); 
	var $components = array('RequestHandler');
	
	function index($khoa_id=null,$namhocht=0,$hockyht=0,$changenh = 0)
	 {
	 	if(!$khoa_id)
	 		$this->redirect(array('controller'=>'pages','action'=>'home'));
	 	$isAjax = 0;
		if($this->RequestHandler->isAjax())
		{
			$this->layout = 'ajax';
			$isAjax = 1;
			if($changenh ==0)
			{
				if(isset($this->data['Phongtraokhoa']['hocky']))
					$hockyht = $this->data['Phongtraokhoa']['hocky'];
				else
				{
					$hockyht = $this->data['hocky'];
				}
			}
				
			$namhocht = $this->data['Phongtraokhoa']['namhoc'];
			if(!empty($hockyht))
			{
				$thoigian = $this->Hocky->find('first',array(
										'recursive'=>-1,
										'conditions'=>array('id'=>$hockyht)));
				$hk =1;
			}
			else
			{
				$hockyht=0;
				$thoigian = $this->Hocky->find('first',array(
										'recursive'=>-1,
										'fields'=>array('min(ngaybatdau) as ngaybatdau','max(ngayketthuc) as ngayketthuc'),
										'conditions'=>array('namhoc_id'=>$namhocht)));
				$hk =0;
			}
			
			$ngaybdhk=null;
			$ngaykthk = null;
			if(!empty($thoigian))
			{
				if ($hk == 1)
				{
					$ngaybdhk = $thoigian['Hocky']['ngaybatdau'];
					$ngaykthk = $thoigian['Hocky']['ngayketthuc'];
				}
				else
				{
					$ngaybdhk = $thoigian[0]['ngaybatdau'];
					$ngaykthk = $thoigian[0]['ngayketthuc']; 
				}
			}
			$this->Phongtraokhoa->recursive = 0;
			$this->set('phongtraokhoas', $this->paginate(array('khoa_id'=>$khoa_id,'ngaybatdau >='=>$ngaybdhk,'OR'=>array('ngayketthuc <='=>$ngaykthk,'ngayketthuc'=>null))));
		}
		else
		{
			if($namhocht==0)
			{
				$ngay= date('Y-m-d');
				$hkht = $this->Hocky->find('first',array(
											'recursive'=>-1,
											'conditions'=>array('Hocky.ngaybatdau <='=>$ngay,
																'Hocky.ngayketthuc >='=>$ngay)));
				$hockyht=0;
				$namhocht = 0;
				$ngaybdhk=null;
				$ngaykthk = null;
				if(!empty($hkht))
				{
					$hockyht = $hkht['Hocky']['id'];
					$namhocht = $hkht['Hocky']['namhoc_id'];
					$ngaybdhk = $hkht['Hocky']['ngaybatdau'];
					$ngaykthk = $hkht['Hocky']['ngayketthuc'];
				}
			}
			else
			{
				if($hockyht==0)
				{
					$thoigian = $this->Hocky->find('first',array(
										'recursive'=>-1,
										'fields'=>array('min(ngaybatdau) as ngaybatdau','max(ngayketthuc) as ngayketthuc'),
										'conditions'=>array('namhoc_id'=>$namhocht)));
					$ngaybdhk = $thoigian[0]['ngaybatdau'];
					$ngaykthk = $thoigian[0]['ngayketthuc'];
				}
				else
				{
					$thoigian = $this->Hocky->find('first',array(
										'recursive'=>-1,
										'conditions'=>array('id'=>$hockyht)));
					$ngaybdhk = $thoigian['Hocky']['ngaybatdau'];
					$ngaykthk = $thoigian['Hocky']['ngayketthuc'];
				}
			}
			$namhocs = $this->Namhoc->find('list',array('fields'=>array('namhoc'),'order'=>array('id DESC')));
			$hockys = $this->Hocky->find('list',array('fields'=>array('tenhocky'),
													'conditions'=>array('namhoc_id'=>$namhocht)));
				
			$this->set(compact('namhocs','hockys','hockyht','namhocht'));
			$this->Phongtraokhoa->recursive = 0;
			$this->set('phongtraokhoas', $this->paginate(array('khoa_id'=>$khoa_id,'ngaybatdau >='=>$ngaybdhk,
																'OR'=>array('ngayketthuc <='=>$ngaykthk,
																			'ngayketthuc'=>null))));	
		}
		$this->set(compact('isAjax','khoa_id','namhocht','hockyht'));
	}

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid phongtraokhoa', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('phongtraokhoa', $this->Phongtraokhoa->read(null, $id));
	}

	function add($khoa_id=null)
	{
		if($khoa_id==null)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		if (!empty($this->data))
			{
				$this->data['Phongtraokhoa']['khoa_id']=$khoa_id;
				$this->data['Phongtraokhoa']['ngaybatdau']=date('Y-m-d',strtotime($this->data['Phongtraokhoa']['ngaybatdau']));
				if(!empty($this->data['Phongtraokhoa']['ngayketthuc']))
					$this->data['Phongtraokhoa']['ngayketthuc']=date('Y-m-d',strtotime($this->data['Phongtraokhoa']['ngayketthuc']));
				else
					$this->data['Phongtraokhoa']['ngayketthuc']=null;
				$this->Phongtraokhoa->create();
				if ($this->Phongtraokhoa->save($this->data)) {
					$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
						$this->redirect(array('action' => 'index',$khoa_id));
				} else {
					$this->Session->setFlash(__('Không Thể Lưu. Vui lòng thử lại', true),'error');
				}
			}
			$caps = $this->Phongtraokhoa->Cap->find('list',array('fields'=>array('tencap')));
				$loaiphongtraos = $this->Phongtraokhoa->Loaiphongtrao->find('list',array('fields'=>array('tenloaiphongtrao')));
			$this->set(compact('caps', 'loaiphongtraos','khoa_id'));
	}

	function edit($khoa_id=null,$id = null) {
		if($khoa_id==null)
			$this->redirect(array('controller'=>'pages','action'=>'home'));
		
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Không Có Phong Trào Này', true));
			$this->redirect(array('action' => 'index',$khoa_id));
		}
		if (!empty($this->data)) {
			
			$this->data['Phongtraokhoa']['khoa_id']=$khoa_id;
			$this->data['Phongtraokhoa']['ngaybatdau']=date('Y-m-d',strtotime($this->data['Phongtraokhoa']['ngaybatdau']));
			if(!empty($this->data['Phongtraokhoa']['ngayketthuc']))
				$this->data['Phongtraokhoa']['ngayketthuc']=date('Y-m-d',strtotime($this->data['Phongtraokhoa']['ngayketthuc']));
			else
				$this->data['Phongtraokhoa']['ngayketthuc']=null;
				
			if ($this->Phongtraokhoa->save($this->data)) {
				$this->Session->setFlash(__('Thông tin đã lưu', true),'success');
				$this->redirect(array('action' => 'index',$khoa_id));
			} else {
				$this->Session->setFlash(__('Không Thể Lưu. Vui Lòng Thử Lại', true),'error');
			}
		}
		if (empty($this->data))
		{
			$this->data = $this->Phongtraokhoa->read(null, $id);
			$this->data['Phongtraokhoa']['ngaybatdau']=date('d-m-Y',strtotime($this->data['Phongtraokhoa']['ngaybatdau']));
			if(!empty($this->data['Phongtraokhoa']['ngayketthuc']))
				$this->data['Phongtraokhoa']['ngayketthuc']=date('d-m-Y',strtotime($this->data['Phongtraokhoa']['ngayketthuc']));
			else
				$this->data['Phongtraokhoa']['ngayketthuc']=null;
		}
		$caps = $this->Phongtraokhoa->Cap->find('list',array('fields'=>array('tencap')));
		$loaiphongtraos = $this->Phongtraokhoa->Loaiphongtrao->find('list',array('fields'=>array('tenloaiphongtrao')));
		$this->set(compact('caps', 'loaiphongtraos','id','khoa_id'));
		
	}

	function delete($khoa_id=null,$id = null) {
		if (!$id && !$khoa_id) {
			$this->Session->setFlash(__('Không Có Phong Trào này', true));
			$this->redirect(array('action'=>'index',$khoa_id));
		}
		if ($this->Phongtraokhoa->delete($id)) {
			$this->Session->setFlash(__('Thông tin đã xóa', true),'success');
			$this->redirect(array('action'=>'index',$khoa_id));
		}
		$this->Session->setFlash(__('Thông tin không thể xóa', true),'error');
		$this->redirect(array('action' => 'index',$khoa_id));
	}

	function updatehk()
	{
		$this->layout= 'ajax'; 
		$namhocht = $this->data['Phongtraokhoa']['namhoc'];
		$hockys = $this->Hocky->find('list',array('fields'=>array('tenhocky'),
													'conditions'=>array('namhoc_id'=>$namhocht)));
		$this->set(compact('hockys'));
	}
	
	function updatept($changenh =0)
	{
		$this->layout = 'ajax';
		
		if($changenh ==0)
		{
			if(isset($this->data['Phongtraokhoa']['hocky']))
				$hocky_id = $this->data['Phongtraokhoa']['hocky'];
			else
			{
				$hocky_id = $this->data['hocky'];
			}
		}
			
		$namhoc_id = $this->data['Phongtraokhoa']['namhoc'];
		if(!empty($hocky_id))
		{
			$thoigian = $this->Hocky->find('first',array(
									'recursive'=>-1,
									'conditions'=>array('id'=>$hocky_id)));
			$hk =1;
		}
		else
		{
			$thoigian = $this->Hocky->find('first',array(
									'recursive'=>-1,
									'fields'=>array('min(ngaybatdau) as ngaybatdau','max(ngayketthuc) as ngayketthuc'),
									'conditions'=>array('namhoc_id'=>$namhoc_id)));
			$hk =0;
		}
		$ngaybdhk=null;
		$ngaykthk = null;
		if(!empty($thoigian))
		{
			if ($hk == 1)
			{
				$ngaybdhk = $thoigian['Hocky']['ngaybatdau'];
				$ngaykthk = $thoigian['Hocky']['ngayketthuc'];
			}
			else
			{
				$ngaybdhk = $thoigian[0]['ngaybatdau'];
				$ngaykthk = $thoigian[0]['ngayketthuc']; 
			}
		}
		$this->Phongtraokhoa->recursive = 0;
		$this->set('phongtraokhoas', $this->paginate(array('ngaybatdau >='=>$ngaybdhk,'OR'=>array('ngayketthuc <='=>$ngaykthk,'ngayketthuc'=>null))));
	}

}
